package com.lin.mybatis.aop;

import com.lin.mybatis.IgnoreTenant;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * @author linjiang
 * @date 2024/7/6
 * @fn
 */
@Slf4j
@Component
@Aspect
public class TenantAop {
    private final static String POINTCUT = "@annotation(com.lin.mybatis.IgnoreTenant)";

    @Before(POINTCUT)
    public void before(JoinPoint point) {
        MethodSignature signature = (MethodSignature) point.getSignature();
        IgnoreTenant tenant = signature.getMethod().getAnnotation(IgnoreTenant.class);
        TenantContext.set(tenant.tenantLine());
    }

    @After(POINTCUT)
    public void after() {
        TenantContext.clear();
    }
}
